package com.l024.loanthief.utils;

import android.util.Log;

import com.l024.loanthief.BuildConfig;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/1/14 10:58
 * @Notes
 */
public class Logutils {
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 信息日志打印
     */
    public static void i(String text){
        if(BuildConfig.LOG_DEBUG){
            if(text!=null&&text.length()>0){
                Log.i(BuildConfig.LOG_TAG,text);
//                witerToFile(text);
            }
        }
    }

    /**
     * 错误日志打印
     */
    public static void e(String text){
        if(BuildConfig.LOG_DEBUG){
            if(text!=null&&text.length()>0){
                Log.e(BuildConfig.LOG_TAG,text);
            }
        }
    }

    /**
     * 写入文件
     */
    public static void witerToFile(String text){
        String fileName = "/sdcard/Meet/Meet.log";
        //时间+内容
        String log = simpleDateFormat.format(new Date())+"  "+text;
        File fileDir = new File("/sdcard/Meet");
        if(!fileDir.exists()){
            fileDir.mkdirs();
        }
        FileOutputStream fos = null;
        BufferedWriter bw = null;
        try {
            fos = new FileOutputStream(fileName,true);
            bw = new BufferedWriter(new OutputStreamWriter(fos, Charset.forName("gbk")));
            bw.write(log);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
